programming4us
           
 
 
Windows Phone

Programming Windows Phone 7: An Introduction to Touch - Low-Level Touch Handling in XNA

- Free product key for windows 10
- Free Product Key for Microsoft office 365
- Malwarebytes Premium 3.7.1 Serial Keys (LifeTime) 2019
1/18/2011 5:12:18 PM
The multi-touch input device is referred to in XNA as a touch panel. You use methods in the static TouchPanel class to obtain this input. Although you can obtain gestures, let’s begin with the lower-level touch information.

It is possible (although not necessary) to obtain information about the multi-touch device itself by calling the static TouchPanel.GetCapabilities method. The TouchPanelCapabilities object returned from this method has two properties:

  • IsConnected is true if the touch panel is available. For the phone, this will always be true.

  • MaximumTouchCount returns the number of touch points, at least 4 for the phone.

For most purposes, you just need to use one of the other two static methods in TouchPanel. To obtain low-level touch input, you’ll probably be calling this method during every call to Update after program initialization:

TouchCollection touchLocations = TouchPanel.GetState();

The TouchCollection is a collection of zero or more TouchLocation objects. TouchLocation has three properties:

  • State is a member of the TouchLocationState enumeration: Pressed, Moved, Released.

  • Position is a Vector2 indicating the finger position relative to the upper-left corner of the viewport.

  • Id is an integer identifying a particular finger from Pressed through Released.

If no fingers are touching the screen, the TouchCollection will be empty. When a finger first touches the screen, TouchCollection contains a single TouchLocationobject with State equal to Pressed. On subsequent calls to TouchPanel.GetState, the TouchLocation object will have State equal to Moved even if the finger has not physically moved. When the finger is lifted from the screen, the State property of the TouchLocation object will equal Released. On subsequent calls to TouchPanel.GetState, the TouchCollection will be empty.

One exception: If the finger is tapped and released on the screen very quickly—that is, within a 1/30th of a second—it’s possible that the TouchLocation object with State equal to Pressed will be followed with State equal to Released with no Moved states in between.

That’s just one finger touching the screen and lifting. In the general case, multiple fingers will be touching, moving, and lifting from the screen independently of each other. You can track particular fingers using the Id property. For any particular finger, that Id will be the same from Pressed, through all the Moved values, to Released.

Very often when dealing with low-level touch input, you’ll use a Dictionary object with keys based on the Id property to retain information for a particular finger.

TouchLocation also has a very handy method called TryGetPreviousLocation, which you call like this:

TouchLocation previousTouchLocation;
bool success = touchLocation.TryGetPreviousLocation(out previousTouchLocation);

Almost always, you will call this method when touchLocation.State is Moved because you can then obtain the previous location and calculate a difference. If touchLocation.State equals Pressed, then TryGetPreviousLocation will return false and previousTouchLocation.State will equal the enumeration member TouchLocationState.Invalid. You’ll also get these results if you use the method on a TouchLocation that itself was returned from TryGetPreviousLocation.

The program I’ve proposed changes the text color when the user touches the text string, so the processing of TouchPanel.GetStates will be relatively simple. The program will examine only TouchLocation objects with State values of Pressed.

This project is called XnaTouchHello. Like the other XNA projects you’ve seen so far, it needs a font, which I’ve made a little larger so it provides a more substantial touch target. A few more fields are required:

Example 1. XNA Project: XnaTouchHello File: Game1.cs (excerpt showing fields)
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;

Random rand = new Random();
string text = "Hello, Windows Phone 7!";
SpriteFont segoe36;
Vector2 textSize;
Vector2 textPosition;
Color textColor = Color.White;
. . .

}

The LoadContent method is similar to earlier versions except that textSize is saved as a field because it needs to be accessed in later calculations:

Example 2. XNA Project: XnaTouchHello File: Game1.cs (excerpt)
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);

segoe36 = this.Content.Load<SpriteFont>("Segoe36");
textSize = segoe36.MeasureString(text);
Viewport viewport = this.GraphicsDevice.Viewport;
textPosition = new Vector2((viewport.Width - textSize.X) / 2,
(viewport.Height - textSize.Y) / 2);
}

As is typical with XNA programs, much of the “action” occurs in the Update method. The method calls TouchPanel.GetStates and then loops through the collection of TouchLocation objects to find only those with State equal to Pressed.

Example 3. XNA Project: XnaTouchHello File: Game1.cs (excerpt)
protected override void Update(GameTime gameTime)
{
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
this.Exit();

TouchCollection touchLocations = TouchPanel.GetState();

foreach (TouchLocation touchLocation in touchLocations)
{
if (touchLocation.State == TouchLocationState.Pressed)
{
Vector2 touchPosition = touchLocation.Position;

if (touchPosition.X >= textPosition.X &&
touchPosition.X < textPosition.X + textSize.X &&
touchPosition.Y >= textPosition.Y &&
touchPosition.Y < textPosition.Y + textSize.Y)
{
textColor = new Color((byte)rand.Next(256),
(byte)rand.Next(256),
(byte)rand.Next(256));
}
else
{
textColor = Color.White;
}
}
}

base.Update(gameTime);
}

If the Position is inside the rectangle occupied by the text string, the textColor field is set to a random RGB color value using one of the constructors of the Color structure. Otherwise, textColor is set to Color.White.

The Draw method looks very similar to the versions you’ve seen before, except that the text color is a variable:

Example 4. XNA Project: XnaTouchHello File: Game1.cs (excerpt)
protected override void Draw(GameTime gameTime)
{
this.GraphicsDevice.Clear(Color.Navy);

spriteBatch.Begin();
spriteBatch.DrawString(segoe36, text, textPosition, textColor);
spriteBatch.End();

base.Draw(gameTime);
}

One problem you might notice is that touch is not quite as deterministic as you might like. Even when you touch the screen with a single finger, the finger might make contact with the screen in more than one place. In some cases, the same foreach loop in Update might set textColor more than once!

Other -----------------
- Windows Phone 7: Responding to a Message
- Windows Phone 7: Checking for New Messages
- Windows Phone 7: Sorting and Searching Your Mail
- Windows Phone 7: Customizing Your Contacts List
- Windows Phone 7: Working with the Me Card
- Windows Phone 7: Posting to Facebook or Windows Live
- Programming Windows Phone 7 : Simple Clocks (part 2)
- Programming Windows Phone 7 : Simple Clocks (part 1)
- Windows Phone7: Pinning a Contact to Start
- Windows Phone7: Adding a Picture or Ringtone to a Contact
- Windows Phone7: Deleting a Contact
- Programming Windows Phone 7: XNA Orientation
- Programming Windows Phone 7: Orientation Events
- Windows Phone 7: Editing a Contact
- Windows Phone 7: Finding a Contact
- Windows Phone 7: Adding a Contact
- Windows Phone 7: Linking Contacts
- Programming Windows Phone 7 : Silverlight and Dynamic Layout (part 2)
- Programming Windows Phone 7 : Silverlight and Dynamic Layout (part 1)
- Programming Windows Phone 7 : An XNA Program for the Phone (part 3)
 
 
 
Top 10
 
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 2) - Wireframes,Legends
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Finding containers and lists in Visio (part 1) - Swimlanes
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Formatting and sizing lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Adding shapes to lists
- Microsoft Visio 2013 : Adding Structure to Your Diagrams - Sizing containers
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 3) - The Other Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 2) - The Data Properties of a Control
- Microsoft Access 2010 : Control Properties and Why to Use Them (part 1) - The Format Properties of a Control
- Microsoft Access 2010 : Form Properties and Why Should You Use Them - Working with the Properties Window
- Microsoft Visio 2013 : Using the Organization Chart Wizard with new data
- First look: Apple Watch

- 3 Tips for Maintaining Your Cell Phone Battery (part 1)

- 3 Tips for Maintaining Your Cell Phone Battery (part 2)
programming4us programming4us